package de.clapp.musicbox.viewpages;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import de.clapp.R;
import de.clapp.musicbox.MusicFragment;
import de.clapp.musicbox.listAdapter.InterpretListAdapter;

@SuppressLint("ValidFragment")
public class InterpretMusicFragmentList extends MusicFragmentList implements OnQueryTextListener
{
    private SearchView mySearchView;

    private InterpretListAdapter adapter;

    public InterpretMusicFragmentList()
    {
        super("Interpret");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        setHasOptionsMenu(true);
        this.adapter = new InterpretListAdapter(inflater.getContext(), this);
        this.setListAdapter(this.adapter);
        // View view = inflater.inflate(R.layout.music_list, container, false);
        
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (this.adapter.getCount() <= 0)
        {
            this.setListShown(false);
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int index, long id)
    {
        this.adapter.setSelectedArtist(index);
        MusicFragment.myViewPager.setCurrentItem(MusicFragment.myViewPager.getCurrentItem() + 1, true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
    {
        MenuInflater menuInflater = getActivity().getMenuInflater();
        menuInflater.inflate(R.menu.menu_menucard, menu);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        this.mySearchView = (SearchView) searchItem.getActionView();

        this.mySearchView.setIconifiedByDefault(false);
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        // searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        this.mySearchView.setOnQueryTextListener(this);

        // mySearchView.setQuery("2", false);
        // mySearchView.setIconified(false);
    }

    @Override
    public boolean onQueryTextChange(String constraint)
    {
        Filter filter = this.adapter.getFilter();
        filter.filter(constraint);
        return true;
    }

    @Override
    public boolean onQueryTextSubmit(String arg0)
    {
        // TODO Auto-generated method stub
        return false;
    }
}
